﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Controllers;

namespace Web
{
    public partial class ShareMenu : System.Web.UI.Page
    {
        protected StringBuilder menuSB = new StringBuilder(@"<script>
                d = new dTree('d', 'dtree');
                d.add(0, -1, '共享文档','ShareList.aspx', '共享文档','mdi');");
        protected IEnumerable<DocumentDTO> docDtoes;
        protected void Page_Load(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            docDtoes = ctx.ExecuteQuery<DocumentDTO>(
                    @"with t as(SELECT Id,Pid,Name,CreatTime,OwnerId,OrderNo FROM Files
  where FileClass=1 and Pid is null and Type='dir'
  union all
  select b.Id,b.Pid,b.Name,b.CreatTime,b.OwnerId,b.OrderNo
  from Files b,t where b.Pid=t.Id and b.Type='dir')
  select * from t;").ToList();
            ConstructMenu(null);
            menuSB.Append(@"d.draw();
            </script>");
        }

        private void ConstructMenu(int? pId)
        {
            IEnumerable<DocumentDTO> selDtoes = docDtoes.OrderBy(r=>r.OrderNo).Where(r => r.Pid == pId);
            foreach(DocumentDTO d in selDtoes)
            {
                menuSB.AppendFormat("d.add({0},{1},'{2}', '{3}', '{4}', 'mdi');",d.Id,d.Pid??0,d.Name,"ShareList.aspx?pId="+d.Id,d.Name);
                ConstructMenu(d.Id);
            }
        }
    }
}